﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using SocialExplorer.IO.FastDBF;

namespace Soil
{
    class Program
    {
        static void Main(string[] args)
        {
            NSDBSoilData nsdb = new NSDBSoilData(
                @"C:\zhiqiang\Elie\Data\ModelInput_New\Soil\CanSIS\dss_v3_mb_20130716\Boyne_Clip",
                "Boyne_Clip.dbf",
                "dss_v3_mb_cmp.dbf",
                "soil_name_mb_v2.dbf",
                "soil_layer_mb_v2.dbf");

            nsdb.GenerateSWATFiles();

            //get all unique soils
            //Dictionary<string, Soil> soils = new Dictionary<string, Soil>();
            //int i = 1;
            //foreach (SoilPolygon p in nsdb.Polygons)
            //{
            //    if (p.MergedComponent != null && p.MergedComponent.Soil != null)
            //    {
            //        if (!soils.ContainsKey(p.MergedComponent.Soil.ID))
            //        {
            //            soils.Add(p.MergedComponent.Soil.ID, p.MergedComponent.Soil);
            //            p.MergedComponent.Soil.LookupIndex = i; //set lookup index
            //            i++;
            //        }
            //    }
            //    else
            //        System.Diagnostics.Debug.WriteLine("No information for polygon " + p.ID);
            //}

            ////write parameters
            //using (StreamWriter w = new StreamWriter(@"C:\zhiqiang\Elie\Data\ModelInput_New\Soil\dss_v3_mb_20130716\test.csv"))
            //{
            //    w.WriteLine(Soil.SWATHeader);
            //    foreach (Soil s in soils.Values)
            //        w.WriteLine(s);
            //}

            ////write lookup table
            //using (StreamWriter w = new StreamWriter(@"C:\zhiqiang\Elie\Data\ModelInput_New\Soil\dss_v3_mb_20130716\lookup.csv"))
            //{
            //    w.WriteLine("Value,Name");
            //    foreach (Soil s in soils.Values)
            //        w.WriteLine(string.Format("{0},{1}", s.LookupIndex, s.Name));              
            //}

            ////write lookup index to shapefile dbf for ArcSWAT
            //var odbf = new DbfFile(Encoding.GetEncoding(1252));
            //odbf.Open("", FileMode.Open);
            //var orec = new DbfRecord(odbf.Header) { AllowDecimalTruncate = true };
            //orec.Clear();
            //orec[0] = "New record added!";
            //orec[6] = "100";
            //orec[8] = "t";
            //odbf.Write(orec, true);

            //odbf.Close();
            //foreach (SoilPolygon p in nsdb.Polygons)
            //    if (p.MergedComponent != null && p.MergedComponent.Soil != null)
            //        p.MergedComponent.Soil.LookupIndex
        }
    }
}
